package com.fanrui.code.chess.mahjong;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.fanrui.code.chess.AbstractChessGameRules;
import com.fanrui.proto.cmd.HuType;
import com.fanrui.proto.cmd.MahjongBaseRoomConfig;
import com.fanrui.proto.cmd.MahjongType;

public class MahjongRules extends AbstractChessGameRules {

	protected MahjongBaseRoomConfig roomConfig;

	private EndType endType = EndType.Normal;

	protected Map<MahjongType, Integer> cards;

	protected Map<HuType, Integer> huTypeCorrespondenceTable;

	protected CurrencyType currencyType = CurrencyType.Score;

	public MahjongRules(MahjongBaseRoomConfig roomConfig, Map<MahjongType, Integer> cards,
			Map<HuType, Integer> huTypeCorrespondenceTable) {
		this.roomConfig = roomConfig;
		this.cards = cards;
		this.huTypeCorrespondenceTable = huTypeCorrespondenceTable;
	}

	public MahjongBaseRoomConfig getRoomConfig() {
		return roomConfig;
	}

	public void setRoomConfig(MahjongBaseRoomConfig roomConfig) {
		this.roomConfig = roomConfig;
	}

	public EndType getEndType() {
		return endType;
	}

	public void setEndType(EndType endType) {
		this.endType = endType;
	}

	public Map<MahjongType, Integer> getCards() {
		return cards;
	}

	public void setCards(Map<MahjongType, Integer> cards) {
		this.cards = cards;
	}

	public Map<HuType, Integer> getHuTypeCorrespondenceTable() {
		return huTypeCorrespondenceTable;
	}

	public void setHuTypeCorrespondenceTable(Map<HuType, Integer> huTypeCorrespondenceTable) {
		this.huTypeCorrespondenceTable = huTypeCorrespondenceTable;
	}

	public CurrencyType getCurrencyType() {
		return currencyType;
	}

	public void setCurrencyType(CurrencyType currencyType) {
		this.currencyType = currencyType;
	}
	
	public int getBaseScore() {
		return roomConfig.getLocalLimit();
	}
	
	/**
	 * 是否超过局数限制
	 * @param currentRound
	 * @return
	 */
	public boolean checkBoard(int currentRound) {
		return roomConfig.getBoardNum() > currentRound;
	}

	@Override
	public int maximumCapacity() {
		return 4;
	}

	public enum EndType {
		/**
		 * 血战
		 */
		XueZhan,
		/**
		 * 血流
		 */
		XueLiu,
		/**
		 * 普通
		 */
		Normal;
	}

	public enum CurrencyType {
		/**
		 * 积分
		 */
		Score,

		/**
		 * 金币
		 */
		Gold;
	}

	public int convertMulAndRemoveType(List<HuType> huTypes) {
		filterType(huTypes);
		return convertMul(huTypes);
	}

	@Override
	public int gamerHandleNum() {
		return 13;
	}
	
	/**
	 * 过滤掉不属于这个规则的牌型
	 * 
	 * @param types
	 *            void
	 */
	public void filterType(Collection<HuType> types) {
		Iterator<HuType> iterator = types.iterator();
		while (iterator.hasNext()) {
			if (!huTypeCorrespondenceTable.containsKey(iterator.next())) {
				iterator.remove();
			}
		}
	}
	
	/**
	 * 将胡牌类型转换成番数
	 * 
	 * @param types
	 * @return int
	 */
	public int convertMul(Collection<HuType> types) {
		int total = 0;
		for (HuType type : types) {
			total += getHutTypeMul(type);
		}
		return total;
	}
	
	/**
	 * 每种胡牌类型对应的点数
	 * 
	 * @param type
	 * @return int
	 */
	public int getHutTypeMul(HuType type) {
		if (!huTypeCorrespondenceTable.containsKey(type)) {
			return 0;
		} else {
			return huTypeCorrespondenceTable.get(type);
		}
	}

}
